package com.example.administrator.jingpingou.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.administrator.jingpingou.R;
import com.example.administrator.jingpingou.bean.CommodityBean;
import com.example.administrator.jingpingou.config.NetConfig;

import org.xutils.x;

import java.util.List;

/**
 * Created by Administrator on 2016/8/30 0030.
 */
public class MineTaoCatAdapter extends BaseAdapter {
    private List<CommodityBean> mData;
    private Context mContext;
    private View.OnClickListener mClickListener;
    private CompoundButton.OnCheckedChangeListener mCheckChangeListener;
    public MineTaoCatAdapter(List<CommodityBean> mData, Context mContext) {
        this.mData = mData;
        this.mContext = mContext;
        mClickListener= (View.OnClickListener) mContext;
        mCheckChangeListener= (CompoundButton.OnCheckedChangeListener) mContext;

    }

    @Override
    public int getCount() {
        return mData==null?0:mData.size();
    }

    @Override
    public Object getItem(int position) {
        return mData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        CommodityBean commodityBean = mData.get(position);
        MyViewHolder holder=null;
        if(convertView==null) {
           convertView= LayoutInflater.from(mContext).inflate(R.layout.item_shopcar,null);
            holder=new MyViewHolder(convertView);
            convertView.setTag(holder);

        }else{
            holder= (MyViewHolder) convertView.getTag();
        }
        holder.tvTitle.setText(commodityBean.getTitle());
        holder.tvPrice.setText(commodityBean.getNow_price());
        holder.tvCount.setText(commodityBean.getCount());
        x.image().bind(holder.ivTitle, NetConfig.TEJIA_ITEM_IAMHRVIEW_PATH+commodityBean.getPic_url());
       holder.btSub.setOnClickListener(mClickListener);
        holder.btAdd.setOnClickListener(mClickListener);
        holder.btDelete.setOnClickListener(mClickListener);
        holder.cb.setOnCheckedChangeListener(mCheckChangeListener);
        holder.cb.setChecked(mData.get(position).isChecked());
        holder.cb.setTag(position);
        holder.btAdd.setTag(position);
        holder.btSub.setTag(position);
        holder.btDelete.setTag(position);
        return convertView;
    }
    public static class MyViewHolder {
        private TextView tvTitle,tvPrice,tvCount;
        private ImageView ivTitle;
        private Button btSub,btAdd,btDelete;
        private CheckBox cb;

        public MyViewHolder(View view) {
            tvTitle= (TextView) view.findViewById(R.id.tv_itemcat_info);
            tvCount= (TextView) view.findViewById(R.id.tv_itemcat_count);
            tvPrice= (TextView) view.findViewById(R.id.tv_itemcat_price);
            ivTitle= (ImageView) view.findViewById(R.id.iv_itemcat_icon);
            btAdd= (Button) view.findViewById(R.id.bt_itemcat_add);
            btSub= (Button) view.findViewById(R.id.bt_itemcat_sub);
            btDelete= (Button) view.findViewById(R.id.bt_itemcat_delete);
            cb= (CheckBox) view.findViewById(R.id.cb_itemcat_check);
        }
    }
    public float getMoney(){
        float money=0;
        if(mData==null&&mData.size()==0) {
            return money;
        }
        for (int i = 0; i <mData.size() ; i++) {
            if(mData.get(i).isChecked()) {
                float price =Float.parseFloat(mData.get(i).getNow_price());
                int count=Integer.parseInt(mData.get(i).getCount());
                price*=count;
                money+=price;

            }
        }
        return money;
    }
}
